package com.sale.plugins;

import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.exception.BusinessException;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.ius.plugin.UtilPlugin;
import com.apache.tools.DataMap;
import com.apache.tools.DateUtils;
import com.apache.tools.NumberUtils;
import com.apache.tools.StrUtil;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 从平台系统规则中查询对应类型交易规则，初始化至发售系统品种的交易规则内。
 */
public class InitTrsRulesProPluginImpl implements CustomMethodPlugin {
    private Logger log = Logger.getLogger(getClass());

    @SuppressWarnings("rawtypes")
    @Override
    public Object beforeInvoke(Map<String, Object> param) {
        //1、查询系统规则
        Map<String, Object> whereMap = new HashMap<>();
        String cateNo = String.valueOf(param.get("sysRulesProd.w_cateNo"));
        String prodId = String.valueOf(param.get("trsRulesPro.prodId"));
        String saleId = String.valueOf(param.get("trsRulesPro.saleId"));////只有处理发行活动规则才有此值
        String rulesType = String.valueOf(param.get("rulesType"));
        if (StrUtil.isNull(cateNo))  throw new BusinessException("cateNo参数不能为空！");
        if (StrUtil.isNull(prodId))  throw new BusinessException("prodId参数不能为空！");
        if (StrUtil.isNull(rulesType))  throw new BusinessException("rulesType参数不能为空！");

        whereMap.put("sysRulesProd.w_cateNo",cateNo);
        whereMap.put("sysRulesProd.w_rulesType",rulesType);
        whereMap.put("orderBy","sysRulesProd.rules_no asc");

        ResultEntity resultEntity1 = IusPluginUtil.getInstance().select("plateform","list","s_sysRulesProd",whereMap);
        if(resultEntity1==null || resultEntity1.getEntity()==null){
            throw new BusinessException("查询系统规则失败！");
        }
        List<DataMap> sysRulesProdList = (ArrayList<DataMap>)resultEntity1.getEntity();
        if(sysRulesProdList==null || sysRulesProdList.size()==0){
            throw new BusinessException("查询系统规则失败！");
        }


        //2、解析数据，拼装批量参数
        param.put("ifBatch","T");//启用-批量操作
        param.put("trsRulesPro.splitAttr","rulesNo");//指定批量数据分割属性名，且参数中必须有
        //以下是拼装批量数据每个元素的值，注意多个数据值以[-#-]分割
        for (DataMap sysRulesMap:sysRulesProdList){
            bulidData(param,sysRulesMap,"constantType");
            bulidData(param,sysRulesMap,"constantStatus");
            bulidData(param,sysRulesMap,"rulesId");
            bulidData(param,sysRulesMap,"rulesType");
            bulidData(param,sysRulesMap,"rulesNo");
            bulidData(param,sysRulesMap,"rulesName");
            bulidData(param,sysRulesMap,"cateId");
            bulidData(param,sysRulesMap,"cateNo");
            bulidData(param,sysRulesMap,"cateName");

            bulidData(param,sysRulesMap,"paramName");
            bulidData(param,sysRulesMap,"paramUnit");
            bulidData(param,sysRulesMap,"showTip");
            bulidData(param,sysRulesMap,"showValid");
            bulidData(param,sysRulesMap,"showFlag");
            bulidData(param,sysRulesMap,"formulaNo");
            bulidData(param,sysRulesMap,"formulaCal");
            bulidData(param,sysRulesMap,"formulaName");
            bulidData(param,sysRulesMap,"formulaSrc");
            bulidData(param,sysRulesMap,"formulaDesc");
            bulidData(param,sysRulesMap,"dealType");
            bulidData(param,sysRulesMap,"orderNo");

            bulidData(param,null,"createTime",DateUtils.Now.fmt_yyyyMMdd_HHmmss());
            bulidData(param,null,"prodId",prodId);
            if(!StrUtil.isNull(saleId)){
                bulidData(param,null,"saleId",saleId);//只有处理发行活动规则才有此值
            }
        }

        //3、批量添加规则
        log.debug("查看拼装的参数：");
        for (String key : param.keySet()) {
            log.debug(key + ":" + param.get(key));
        }

        //4、统一有ius接口批量提交

        return null;
    }

    protected void bulidData(Map<String, Object> newMap, DataMap dataMap,String filedName,String... staticVal){
        String newVal = "";
        if(dataMap==null){
            newVal = StrUtil.doNull(staticVal[0],"");
        }else{
            newVal = String.valueOf(dataMap.get(filedName));
        }
        String oldVal = String.valueOf(newMap.get("trsRulesPro."+filedName));
        newMap.put("trsRulesPro."+filedName, strTools(oldVal, newVal));
    }
    protected String strTools(String str, String sufix) {
        sufix = StrUtil.doNull(sufix, "_");
        return StrUtil.isNull(str) ? sufix : str + "-#-" + sufix;
    }

    @Override
    public Object doInvoke(Map<String, Object> param) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> param) {

        // TODO Auto-generated method stub
        return null;
    }

    /**
     * 规则校验入口（专门给系统公式规则提供的）
     *
     * @param paramsVo
     */
    public void run(ParamsVo paramsVo) {
        beforeInvoke(paramsVo.getParams());
    }
}
